# -*- coding:utf-8 -*-

from PyQt5.QtCore import QThread, pyqtSignal
import threading
from time import sleep as tsleep
import traceback


# 使用信号和槽来更新tablewidget
class MyTabThread(QThread):
    mysingal = pyqtSignal(int, int, str, str)  # 括号里填写需要发射的数据类型

    def mysignalEmit(self, i, j, meg, color):
        try:
            self.mysingal.emit(i, j, meg, color)  # 朝connect函数发射一个xxx（没想好）
        except(Exception):
            traceback.print_exc()

    def __init__(self, *args, **kwargs):  # 这里自己实现
        super(MyTabThread, self).__init__(*args, **kwargs)
        self.__flag = threading.Event()  # 用于暂停线程的标识
        self.__flag.set()  # 设置为true
        self.__running = threading.Event()  # 用于停止线程的标识
        self.__running.set()  # 将其设置为true

    def run(self):
        try:
            print("Begin MyTableThread class")
            while self.__running.isSet():  # 如果被设置了true就继续，false就终止了
                self.__flag.wait()  # 为true时立即范围，为Flase时阻塞直到内部标识为Treu后返回
                tsleep(0.5)
            print("Exit MyTableThread class")
        except(Exception):
            traceback.print_exc()

    def pause(self):
        self.__flag.clear()  # 设置为flase，让线程阻塞

    def resume(self):
        self.__flag.set()  # 设置为True，恢复运行

    def stop(self):
        try:
            self.__flag.set()  # 将线程从暂停状态恢复，如果其处于暂停状态
            self.__running.clear()  # 设置为Flase
        except(Exception):
            traceback.print_exc()

